package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinog.biz.mapper.havexml.other.WorkMapper;
import com.sinog.biz.service.business.VerificationService;
import com.sinog.biz.service.business.WorkService;
import com.sinog.biz.util.ConditionUtils;
import com.sinog.biz.util.smlib.Sm4;
import com.sinog.core.util.AssemblyConversionUtils;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @desc 首页 - 工作台 - 工作台全部查询
 * @author lk
 * @date - 2020-09-19-14:02
 */
@Slf4j
@Service
public class WorkServiceImpl implements WorkService {

    @Autowired
    private VerificationService verificationService;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 首页 - 工作台 - 查询工作台全部状态的信息
     * @param searchKey 查询条件
     * pageIndex 起始页 默认:0
     * pageSize 止页数 默认:50
     * sexKey 性别 1:男、2:女
     * state 1:待核查 2:核查中 3:核查成功人员 4:核查失败人员 5:待办工作 6:已办工作 7、上报人员 8、待比对人员
     * nameKey 姓名
     * hjKey 户籍类型 01:户籍完整人员、02:查不清户籍人员
     * oldKey 1:是、2:否
     * zdbjKey 重点帮教对象 1:是、2:否
     * shenKey 身份证
     * zmKey 罪名
     * fxKey 服刑单位
     * qitaKey 其他条件
     * nativepro 省
     * nativecity 市
     * nativecou 区
     * nativevill 街道
     * rjStartDate 原判刑期起日
     * rjEndDate 原判刑期止日
     * xxqzrKey 现刑期止日
     * prisonerState 服刑人员状态
     * @return String
     */
    @Override
    public String getVerificationWorkList(Map<String,String> searchKey) {
        int pageIndex = Integer.parseInt(searchKey.get("pageIndex"));
        int pageSize = Integer.parseInt(searchKey.get("pageSize"));
        String state = searchKey.get("state");
        String nameKey = searchKey.get("nameKey");
        String hjKey = searchKey.get("hjKey");
        String inputSource = searchKey.get("inputSource");
        String sexKey = searchKey.get("sexKey");
        String zdbjKey = searchKey.get("zdbjKey");
        String shenKey = searchKey.get("shenKey");
        String zmKey = searchKey.get("zmKey");
        String fxKey = searchKey.get("fxKey");
        String nativepro = searchKey.get("nativepro");
        String nativecity = searchKey.get("nativecity");
        String nativecou = searchKey.get("nativecou");
        String nativevill = searchKey.get("nativevill");
        String rjStartDate = searchKey.get("rjStartDate");
        String rjEndDate = searchKey.get("rjEndDate");
        String qitaKeyStr = searchKey.get("qitaKey");
        String hcType = searchKey.get("hcType");
        //服刑人员状态，前台可能多选，传递过来形式未00,01,02...
        String prisonerStateStr = searchKey.get("prisonerState");
        //用户信息
        String sortField = searchKey.get("sortField");
        String sortOrder = searchKey.get("sortOrder");
        String departid = searchKey.get("departid");
        String level = searchKey.get("level");
        String account = searchKey.get("account");
        if(Constants.NumberStr.NUM_STR_ZERO.equals(state)) {
            Map<String,Object> codeMap = AssemblyConversionUtils.transferAssembly(level);
            Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
            page.setSearchCount(false);
            //查询全部
            if(StringUtils.isNotBlank(shenKey)) {
                searchKey.put("shenKey",Sm4.encryptEcb(shenKey));
            }
            //工作台状态查询需求特殊处理=====================by pzc===============start
            Long total = 0L;
            List<Map<String,String>> works = new ArrayList<>(10);
            if(StringUtils.isNotBlank(prisonerStateStr)) {
                //如果需要查询服刑人员状态
                //将前台传递的状态00,01,02...字符串拆分成[00 01 02]类型的数组
                String[] prisonerStateArray = prisonerStateStr.split(",");
                for(String prisonerState : prisonerStateArray) {
                    //遍历每一种状态
                    //将罪犯状态放入searchMap中，挨个查，然后将结果拼接并返回
                    searchKey.put("prisonerState",prisonerState);
                    total += workMapper.getCountWorks(searchKey,codeMap);
                    //不管怎么查，都从第一页开始查
                    page.setCurrent(1);
                    //pageSize设置为-1表示查询全部数据
                    page.setSize(-1);
                    //查处符合条件的全部数据
                    List<Map<String,String>> temp = workMapper.getWorks(page,searchKey,codeMap);
                    //将所有集合放入workTemp中
                    works.addAll(temp);
                }
                //状态分页起始位置
                int start = pageSize * pageIndex;
                //所有条件总记录数临时
                int totalTemp = total.intValue();
                int end = Math.min((totalTemp - start),pageSize);
                works = works.subList(start,start + end);
            } else {
                total = workMapper.getCountWorks(searchKey,codeMap);
                works = workMapper.getWorks(page,searchKey,codeMap);
            }
            //工作台状态查询需求特殊处理=====================by pzc===============end
            //把监狱与和对应的省份匹配
            Map<Object,Object> mapProvince = stringRedisTemplate.opsForHash().entries("province");
            for(Map<String,String> item : works) {
                if(StringUtils.isEmpty(item.get("pdanweiName"))) {
                    item.put("pdanweiName","");
                } else if(mapProvince.get(item.get("prinum").substring(0,2) + "0000")!=null) {
                    item.put("pdanweiName",mapProvince.get(item.get("prinum").substring(0,2) + "0000") + "-" + item.get("pdanweiName"));
                }
            }
            searchKey.put("total",String.valueOf(total));
            CompletableFuture.runAsync(()->{
                ConditionUtils.fzSearchKey(searchKey,redisTemplate);
            });
            return JsonUtil.toText(total,works);
        } else {
            //待核查:
            return verificationService.getVerificationList(pageIndex,pageSize,sortField,sortOrder,departid,level,state,sexKey,nameKey,hjKey,inputSource,account,hcType,zdbjKey,shenKey,zmKey,fxKey,qitaKeyStr,nativepro,nativecity,nativecou,nativevill,rjStartDate,rjEndDate);
        }
    }
}